﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bxml.betterxml.xelement
{

    public class XPCData : XNode
    {
        public const String NAME = "PCDATA";

        private String text;

        public XPCData()
            : this("")
        {

        }


        public XPCData(String text)
        {
            this.text = text;
            this.name = NAME;
            initialize();
        }

        /**
         * This function is so that you don't have to overwrite the constructor. Anything you would like 
         * implemented at construction you should put in <code>initialize()</code>. It is always called at 
         * construction.
         */
        public void initialize() { }


        public String getText()
        {
            return text;
        }

        public void trimWhiteSpace()
        {
            text = text.Trim();
        }

        public void setText(String text)
        {
            this.text = text;
        }

        public void appendText(String newText)
        {
            this.text = this.text + newText;
        }

        public String toString()
        {
            return "PCDATA\n\tParent: " + parent.getName() + "\n\t" + text;
        }

        protected String convertEntities()
        {
            return convertEntities(this.text);
        }

        protected String convertEntities(String text)
        {
            return text.Replace("&", "&amp;")
                .Replace(">", "&gt;")
                .Replace("<", "&lt;");

        }

        public override void acceptContentHandler(BetterXmlContentHandler contentHandler)
        {
            contentHandler.characters(text.Length, text);
        }

    }
}
